<?php
session_start();
define ("MAXYEAR",date("Y")-5); // This year, minus 5
define ("MINYEAR",date("Y")-80); // This year, minus 80
require_once 'include/db.php';
require_once 'include/userClass.php';
require_once 'include/functions.php';

//Header variables:
$title = "Endre profil";

// If form not filled out already:
if(!isset($_POST['email'])) {
	include('include/header.php');
	echo "<div id='content'>";
	echo "<h2>Endre brukerprofil</h2>";
}
// If the old password is NOT correct:
elseif (!$user->correctPass($_POST['oldpwd'])) {
    include('include/header.php');
	echo "<div id='content'>";
    echo "<h2>Registrer ny bruker</h2>\n
        <script type='text/javascript'>
            alert('*Gammelt passord stemmer ikke');
        </script>";
}
// If the new password dosn't match the check:
else if ($_POST['pwd1'] != $_POST['pwd2']) {
	include('include/header.php');
	echo "<div id='content'>";
    echo "<h2>Endre ny brukerprofil</h2>\n
        <script type='text/javascript'>
            alert('*Passord må være like');
        </script>";
}
// Form is corrctly filled out:
else {         
	$user->editUser($_FILES, $_POST);
    header("location:displayUsr.php?usrName=$user->userName"); 
}
?>

<form name="userForm" id="userForm" action="editUser.php" method="post" enctype="multipart/form-data">
    <fieldset>
    <legend>Endre passord</legend>
        <label>Gammelt passord</label>
            <input type="password" name="oldpwd" size="22"><br/>
        <label>Nytt passord</label>
            <input type="password" name="pwd1" size="22"
                    pattern="^[ÆØÅæøåa-zA-Z0-9]{8,20}$"
                    title="Brukes for &aring; logge p&aring; senere, m&aring; best&aring; av 8 til 20 tegn (a-z,0-9)"><br/>
        <label>Gjenta nytt passord</label>
            <input type="password" name="pwd2" size="22">
    </fieldset>
    
    <fieldset>
    <legend>Personlig Info</legend>
        <label>Navn</label>
            <input type="text" name="name" size="32" pattern="[ÆØÅæøåa-åA-Å -]+"
                    title="Kan kun best&aring; av bokstaver, mellomrom og bindestrek"
                    value ="<?php if (isset($_POST['name'])) echo $_POST['name'];
                            elseif (isset($user->userRealName) && $user->userRealName != NULL)
                            echo $user->userRealName; ?>"><br/>
        <label>Fødselsår</label>
            <select  name="birth" title="Velg ditt f&oslash;dsels&aring;r">
            <?php if(!isset($_POST['birthyear'])) {
                echo "<option selected disabled hidden value=''></option>";
            }
            for($i = MINYEAR; $i <= MAXYEAR; $i++) {
                if(isset($_POST['birth']) && $_POST['birth'] == $i ||
                   isset($user->birthYear) && $user->birthYear == $i) {
                    echo "<option value='$i' selected>$i</option>";
                }
                else {
                    echo "<option value='$i' >$i</option>";	
                }
            } ?>
            </select><br/>
        <label>E-post*</label>
            <input type="text" name="email" required
                    title="E-post m&aring; v&aelig;re p&aring; gyldig format"
                    value="<?php if (isset($_POST['email'])) echo $_POST['email'];
                    elseif(isset($user->email)) echo $user->email; ?>"><br/>
        <label>Hjemsted</label>
            <input type="text" name="homeTown" size="32" pattern="[ÆØÅæøåa-åA-Å -]+"
                    title="Kan kun best&aring; av bokstaver"
                    value="<?php if (isset($_POST['homeTown'])) echo $_POST['homeTown'];
                    elseif(isset($user->homeTown) && $user->homeTown != NULL)
                    echo $user->homeTown; ?>"><br/>
        <label>Tema</label>
            <select name='theme'>
                <?php if(isset($_POST['theme'])) $themeop = $_POST['theme'];
                elseif(isset($user->theme)) $themeop = $user->theme;
                else $themeop = NULL;
                getUserThemeList($db, $themeop); ?>
            </select>
    </fieldset>
    
    <fieldset>
    <legend>Bilde</legend>
        <label>Kun JPEG-, GIF- and PNG-filer!</label><br>
            <input type="file" id="usrImg" name="usrImg" /><br/>
    </fieldset>
    
    <div id="formend">
        <input type="submit" value="Submit">
        <input type="reset" value="Reset">
        <p>* Påkrevde felter</p>
    </div>
    </form>
    </div>
	
    <div id="side">
	<h2>Informasjon</h2>
	<ul>
	    <li>Felter merket * må være fylt ut</li>
	    <li>Du kan endre passord ved å oppgi det gamle og fylle inn et nytt</li>
	    <li>E-posten din vil <b>ikke</b> vises offentlig på siden</li>
	    <li>Du kan fjerne informasjon om deg selv ved å la feltet være blankt</li>
        <li>Du kan laste opp et nytt profilbilde i JPEG, GIF eller PNG</li>
	</ul>   
    </div>
    
<?php
include('include/footer.php');
?>